home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UUPC11QS.ARJ / NDIROS2.C < prev    next >
C/C++ Source or Header  |  1991-07-10  |  5KB  |  152 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    ndir.c for MS-DOS by Samuel Lam <skl@van-bc.UUCP>, June/87      */
  3. /*    ndir.c for MS-OS2 by Drew Derbyshire (help@kendra.kew.com>,     */
  4. /*           April/91                                                 */
  5. /*                                                                    */
  6. /*         Berkeley-style directory reading routine on MS-OS2         */
  7. /*--------------------------------------------------------------------*/
  8.  
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <assert.h>
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                         OS/2 include files                         */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. #define INCL_BASE
  20. #include <os2.h>
  21.  
  22. /*--------------------------------------------------------------------*/
  23. /*                    UUPC/extended include files                     */
  24. /*--------------------------------------------------------------------*/
  25.  
  26. #include "lib.h"
  27. #include "ndir.h"
  28.  
  29. static HDIR dir_handle;
  30. static char *pathname = NULL;
  31. static struct _FILEFINDBUF findbuf;
  32.  
  33. /*--------------------------------------------------------------------*/
  34. /*    o p e n d i r                                                   */
  35. /*                                                                    */
  36. /*    Open a directory                                                */
  37. /*--------------------------------------------------------------------*/
  38.  
  39. extern DIR *opendirx( char *dirname, char *pattern)
  40. {
  41.  
  42.    DIR *dirp;
  43.    USHORT rc;
  44.    USHORT count = 1;
  45.  
  46.    pathname = malloc( strlen( dirname ) + strlen( pattern ) + 2 );
  47.    strcpy(pathname, dirname);
  48.  
  49.    if ((*pattern != '/') || (dirname[ strlen(dirname) - 1] != '/'))
  50.       strcat(pathname,"/");
  51.    strcat(pathname,pattern);
  52.    printmsg(5,"opendir: Opening directory %s", pathname );
  53.  
  54. /*--------------------------------------------------------------------*/
  55. /*                Read the first file in the directory                */
  56. /*--------------------------------------------------------------------*/
  57.  
  58.    dir_handle = HDIR_CREATE;
  59.    rc = DosFindFirst( pathname,
  60.             &dir_handle,
  61.             FILE_NORMAL,
  62.             &findbuf,
  63.             sizeof( findbuf ),
  64.             &count,
  65.             0L );
  66.  
  67. /*--------------------------------------------------------------------*/
  68. /*            Process the return code from the first file             */
  69. /*--------------------------------------------------------------------*/
  70.  
  71.    if ( rc == 0 )
  72.    {
  73.       dirp = malloc( sizeof( DIR ));
  74.       dirp->dirfirst = 1;
  75.       strcpy(dirp->dirid, "DIR");
  76.       return dirp;
  77.    }
  78.    else {
  79.       if ( rc != ERROR_NO_MORE_FILES )
  80.          printmsg(2,"opendir: Error %d on directory %s",
  81.                   (int) rc, pathname );
  82.       return NULL;
  83.    } /* else */
  84.  
  85. } /*opendir*/
  86.  
  87. /*--------------------------------------------------------------------*/
  88. /*    r e a d d i r                                                   */
  89. /*                                                                    */
  90. /*    Get next entry in a directory                                   */
  91. /*--------------------------------------------------------------------*/
  92.  
  93. struct direct *readdir(DIR *dirp)
  94. {
  95.    USHORT rc = 0;
  96.    USHORT count = 1;
  97.  
  98.    assert(strcmp(dirp->dirid, "DIR") == 0);
  99.  
  100.    if (dirp->dirfirst)
  101.    {
  102.       printmsg(5,"readdir: Opening directory %s", pathname );
  103.       dirp->dirfirst = 0;
  104.    }
  105.    else
  106.       rc = DosFindNext( dir_handle,
  107.                &findbuf,
  108.                sizeof( findbuf ) ,
  109.                &count );
  110.  
  111.    if ( rc == 0 )
  112.    {
  113.       dirp->dirent.d_ino = -1;   /* no inode information */
  114.       strlwr(strcpy(dirp->dirent.d_name, findbuf.achName ));
  115.       dirp->dirent.d_namlen = findbuf.cchName;
  116.       dirp->dirent.d_reclen = sizeof(struct direct) - (MAXNAMLEN + 1) +
  117.          ((((dirp->dirent.d_namlen + 1) + 3) / 4) * 4);
  118.       return &(dirp->dirent);
  119.    }
  120.    else {
  121.       if ( rc != ERROR_NO_MORE_FILES )
  122.          printmsg(0,"readdir: Error %d on directory %s",
  123.                   (int) rc, pathname );
  124.       return NULL;
  125.    } /* else */
  126.  
  127. } /*readdir*/
  128.  
  129. /*--------------------------------------------------------------------*/
  130. /*    c l o s e d i r                                                 */
  131. /*                                                                    */
  132. /*    Close a directory                                               */
  133. /*--------------------------------------------------------------------*/
  134.  
  135. void closedir(DIR *dirp)
  136. {
  137.    USHORT rc;
  138.  
  139.    assert(strcmp(dirp->dirid, "DIR") == 0);
  140.  
  141.    printmsg(5,"closedir: Closing directory %s", pathname );
  142.    rc = DosFindClose( dir_handle );
  143.    if ( rc != 0 )
  144.      printmsg(0,"closedir: Error %d on directory %s",
  145.               (int) rc, pathname );
  146.    free( dirp );
  147.    dirp = NULL;
  148.    free( pathname );
  149.    pathname = NULL;
  150.  
  151. } /*closedir*/
  152.